package com.tensquare.user.controller;
import java.util.HashMap;
import java.util.Map;

import com.tensquare.entity.PageResult;
import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import com.tensquare.util.JwtUtil;
import com.tensquare.util.NotNullHelps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.tensquare.user.model.Admin;
import com.tensquare.user.service.AdminService;


@RestController
@CrossOrigin
@RequestMapping("/admin")
public class AdminController {

	private String message ;

	@Autowired
	private JwtUtil jwtUtil ;

	@Autowired
	private AdminService adminService;

	private Result result = new Result(true, StatusCode.OK);

	@RequestMapping(value="/{id}",method= RequestMethod.DELETE)
	public Result delete(@PathVariable String id ){
		adminService.deleteById(id);
		return new Result(true,StatusCode.OK,"删除成功");
	}


	@RequestMapping(value="/{id}",method= RequestMethod.PUT)
	public Result update(@RequestBody Admin admin, @PathVariable String id ){
		admin.setId(id);
		adminService.update(admin);
		return new Result(true,StatusCode.OK,"修改成功");
	}

	@RequestMapping(value="/search",method = RequestMethod.POST)
	public Result findSearch( @RequestBody Map searchMap){
		return new Result(true,StatusCode.OK,"查询成功",adminService.findSearch(searchMap));
	}

	@RequestMapping(value="/search/{page}/{size}",method=RequestMethod.POST)
	public Result findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){
		Page<Admin> pageList = adminService.findSearch(searchMap, page, size);
		return  new Result(true,StatusCode.OK,"查询成功",  new PageResult<Admin>(pageList.getTotalElements(), pageList.getContent()) );
	}

	@RequestMapping(method=RequestMethod.POST)
	public Result add(@RequestBody Admin admin  ){
		adminService.saveAdmin(admin);
		return new Result(true,StatusCode.OK,"增加成功");
	}

	@RequestMapping(value = "/login",method = RequestMethod.POST)
	public Result loginAdmin(@RequestBody Admin admin){
		admin = adminService.loginAdmin(admin);
		if (NotNullHelps.isNotNull(admin) == false) {
			result = new Result(false, StatusCode.LOGINERROR);
			message = "登录异常--admin";
		} else {
			String jwtToken = jwtUtil.createJWT(admin.getId(), admin.getLoginname(), "admin");
			Map<String, String> map = new HashMap<>();
			message = "admin----登录成功";
			map.put("token", jwtToken);
			map.put("role", "admin");
			result.setData(map);
		}
		result.setMessage(message);
		return result ;
	}

	@RequestMapping(method= RequestMethod.GET)
	public Result findAll(){
		return new Result(true, StatusCode.OK,"查询成功",adminService.findAll());
	}


	@RequestMapping(value="/{id}",method= RequestMethod.GET)
	public Result findById(@PathVariable String id){
		return new Result(true,StatusCode.OK,"查询成功",adminService.findById(id));
	}



}
